﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTCategoryUI : System.Web.UI.Page
    {
        private String message = "";
        private DataTable dtProductCategorys = new DataTable();
        private Int32 DanhMucID = HangSo.DEFAULT_ID;
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dtProductCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhams(-1, "", "").Tables[0];                
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    DanhMucID = Int32.Parse(Request.QueryString["DanhMucID"]);
                }
                catch
                {

                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);

                CreateDDLCategory(0, 0);
                if(IsVN==true)
                    this.ddanhMucCha.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                else
                    this.ddanhMucCha.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));

                if (DanhMucID != -1)
                {
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Edit_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Edit_Other;
                    DanhMucSanPham theDanhMucSanPham = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(DanhMucID);
                    txtTenDanhMucOther.Text = theDanhMucSanPham.TenDanhMucOther;
                    txtTenDanhMucVN.Text = theDanhMucSanPham.TenDanhMucVN;
                    txtThuTu.Text = theDanhMucSanPham.SapXep.ToString();
                    ddanhMucCha.SelectedValue = theDanhMucSanPham.ChuyenMucChaID.ToString();
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["DeletedFlag"] = theDanhMucSanPham.DeletedFlag.ToString();
                    ViewState["DanhMucID"] = DanhMucID;
                }
                else
                {
                    ViewState["action"] = HangSo.ACTION_ADD;
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Add_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Add_Other;
                }

            }
        }

              /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbSave.Text = rm.GetString("lbSave", ci);
            lbCancel.Text = rm.GetString("lbCancel", ci);
            lbApply.Text = rm.GetString("lbApply", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbCategory.Text = rm.GetString("lbCategory", ci);
            lbThuTu.Text = rm.GetString("lbThuTu", ci);
            lbDanhMucCha.Text = rm.GetString("lbDanhMucCha", ci);
            lbTenDanhMucOther.Text = rm.GetString("lbTenDanhMucOther", ci);
            lbTenDanhMucVN.Text = rm.GetString("lbTenDanhMucVN", ci);
           
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucID"].ToString());
                ListItem li;
                if(IsVN==true)
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucVN"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
                else
                     li = new ListItem(preString + drCategorysSub[i]["TenDanhMucOther"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
                this.ddanhMucCha.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }



        /// <summary>
        /// Ham kiem tra du lieu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public Boolean ValidateData()
        {
            Boolean flag = true;

            //if (txtTenDanhMucOther.Text == "")
            //{
            //    if (IsVN == true)
            //        message = HangSo.check_VN;
            //    else
            //        message = HangSo.check_Other;
            //    flag = false;
            //}
            if (txtTenDanhMucVN.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            return flag;
        }


        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 DanhMucIDOut = -1;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                DanhMucSanPham theDanhMucSanPham = new DanhMucSanPham();
                theDanhMucSanPham.TenDanhMucOther = txtTenDanhMucOther.Text.Trim();
                theDanhMucSanPham.TenDanhMucVN = txtTenDanhMucVN.Text.Trim();
                theDanhMucSanPham.IconTrangThai = "~//images//tick.png";
                theDanhMucSanPham.ChuyenMucChaID = Int32.Parse(ddanhMucCha.SelectedValue);                
                if (ddanhMucCha.SelectedValue == "0")
                    theDanhMucSanPham.MenuLevel = 1;
                else
                {
                    DanhMucSanPham theDanhMucSP = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(ddanhMucCha.SelectedValue));
                    if (theDanhMucSP.MenuLevel == 1)
                        theDanhMucSanPham.MenuLevel = 2;
                    else if (theDanhMucSP.MenuLevel == 2)
                        theDanhMucSanPham.MenuLevel = 3;
                    else
                    {
                        if(IsVN==true)
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.meagessErorrAdd);
                        else
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.meagessErorrAddOther);
                        return;
                    }
                        
                }
                try
                {
                    theDanhMucSanPham.SapXep = Int32.Parse(txtThuTu.Text);
                }
                catch
                {
                    if(IsVN)
                        ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.messageNumBer);
                    else
                        ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.messageNumBerOther);
                    return;
                }

                if (action == HangSo.ACTION_ADD)
                {
                    theDanhMucSanPham.DanhMucID = -1;
                    DanhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhMucSanPham, true);
                }
                else
                {
                    theDanhMucSanPham.DanhMucID = Int32.Parse(ViewState["DanhMucID"].ToString());
                    DanhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhMucSanPham, false);
                }

                Response.Redirect("EHTCategory.aspx");
            }
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 DanhMucIDOut = -1;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                DanhMucSanPham theDanhMucSanPham = new DanhMucSanPham();
                theDanhMucSanPham.TenDanhMucOther = txtTenDanhMucOther.Text.Trim();
                theDanhMucSanPham.TenDanhMucVN = txtTenDanhMucVN.Text.Trim();
                theDanhMucSanPham.IconTrangThai = "~//images//tick.png";
                theDanhMucSanPham.ChuyenMucChaID = Int32.Parse(ddanhMucCha.SelectedValue);               
                if (ddanhMucCha.SelectedValue == "0")
                    theDanhMucSanPham.MenuLevel = 0;
                else
                {
                    DanhMucSanPham theDanhMucSP = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(ddanhMucCha.SelectedValue));
                    if (theDanhMucSP.MenuLevel == 1)
                        theDanhMucSanPham.MenuLevel = 2;
                    else if (theDanhMucSP.MenuLevel == 2)
                        theDanhMucSanPham.MenuLevel = 3;
                    else
                    {
                        if (IsVN == true)
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.meagessErorrAdd);
                        else
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.meagessErorrAddOther);
                        return;
                    }
                }
                try
                {
                    theDanhMucSanPham.SapXep = Int32.Parse(txtThuTu.Text);
                }
                catch
                {
                    if (IsVN)
                        ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.messageNumBer);
                    else
                        ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.messageNumBerOther);
                    return;
                }

                if (action == HangSo.ACTION_ADD)
                {
                    theDanhMucSanPham.DanhMucID = -1;
                    DanhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhMucSanPham, true);
                }
                else
                {
                    theDanhMucSanPham.DanhMucID = Int32.Parse(ViewState["DanhMucID"].ToString());
                    DanhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhMucSanPham, false);
                }

                String str = "";
                if (IsVN == true)
                    str = HangSo.App_VN;
                else
                    str = HangSo.App_Other;

                Response.Redirect("EHTCategoryUI.aspx?DanhMucID=" + ViewState["DanhMucID"].ToString() + "&&Message="+str);
            }
        }
    }
}